package org.huangrui.juc.source.rwlock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Author hr
 * @Create 2024-04-23 15:22
 */
public class T02_LockDownGrading {
    public static void main(String[] args) {
        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

        ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
        ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();

        //正常 A B两个线程
        // A
        /*readLock.lock();
        System.out.println("----读取");
        readLock.unlock();

        // B
        writeLock.lock();
        System.out.println("----写入");
        writeLock.unlock();*/


        //本例，only one 同一个线程


        writeLock.lock();
        System.out.println("----写入");

        readLock.lock();
        System.out.println("----读取");
        readLock.unlock();


        /**
         * biz
         *  .....
         */


        writeLock.unlock();
    }
}
